#include "density_set.h"

Density_set::Density_set(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Label_Init();
    ui_Btn_Init();
   connect_Init();
}
void Density_set::connect_Init()
{

    //确定
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        keyPad *board = new keyPad(this);
         board->show();
        connect(board,&keyPad::changename2,this,&Density_set::Recvie_O3qxdd);
        connect(board,&keyPad::destroyed,[=](){
            btnlist_ui.at(1)->setFocus();
        });

    });

    //返回
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });

}
void Density_set::ui_Label_Init()
{

    for(int i=0;i<6;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 20px;\
                                color: #123567;			                 /* 文本颜色 */\
                           }"
          );
    }
    theLabel[0]->setGeometry(100,120,100,40);//设定每隔放置一个按键
    theLabel[1]->setGeometry(360,120,100,40);//设定每隔放置一个按键
    theLabel[2]->setGeometry(250,120,100,40);//设定每隔放置一个按键

    theLabel[3]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
    theLabel[3]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[4]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[5]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[3]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[4]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[5]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[3]->setText("自定义O3灯设置");
   theLabel[0]->setText(tr("当前为："));
   theLabel[1]->setText(tr("%"));
   theLabel[2]->setNum(Sysset->Ozo_Span[O3_densityNum-1]);//显示手动臭氧数据

   Sysset->Sys_Run[1]=Sysctrl->PhotoMeter_Mode;
   switch (Sysset->Sys_Run[1])
   {
   case 0:
   {
       theLabel[1]->setText("%");
       theLabel[2]->setNum(Sysset->Ozo_Span[O3_densityNum-1]);
      break;
   }
   case 1:
   {
       theLabel[1]->setText("%");
       theLabel[2]->setNum(Sysset->Ozo_Span[O3_densityNum-1]);
       break;
   }
   case 2:
   {
       theLabel[1]->setText("%");
       theLabel[2]->setNum(Sysset->Ozo_Span[O3_densityNum-1]);
       break;
   }
   case 3:
   {
       theLabel[1]->setText("PPM");
       theLabel[2]->setNum(Sysset->Set_Ozo_Con[O3_densityNum-1]);
//        if(Sysset->Sys_Ozo_Sel!=0)
//            ui->btn_O3_2->setText(QString("%1").arg(Sysset->Set_Ozo_Con[Sysset->Sys_Ozo_Sel-1]));
//        else
//            ui->btn_O3_2->setText("-----");
       break;
   }
   default:
       break;
   }


}

void Density_set::ui_Btn_Init()
{

    for(int i=0;i<2;i++)//创建主屏幕显示的按键
    {

         theBtn[i] = new QPushButton(this);
//        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
        theBtn[i]->geometry();
        theBtn[i]->setStyleSheet("QPushButton{\
                               outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                               background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                               border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                               border-style: outset;\
                               /* 按钮背景图标设置 */\
                               background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                               background-origin: content;\
                               background-position: top;	         /* 背景图片的位置 */\
                               padding-right: 0px;	    /*内边距设置*/	    \
                               padding-left: 0px;		/*  */	\
                               padding-bottom: 10px;		/*  */    \
                               padding-top: 10px;		/*  */    \
                               background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                               /* 按钮文本设置 */\
                               text-align: bottom;			             /* 文本的位置 */\
                               font-size: 20px;\
                               color: #FFFFFF;			                 /* 文本颜色 */\
                          }"
    "QPushButton:focus{\
outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
    background-color: rgb(255,125,25);	    /* 背景颜色 */\
    border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
    border-style: outset;\
    /* 按钮背景图标设置 */\
    background-image: url(:/pic/access/过滤.png); /* 背景图片 */\
    background-origin: content;\
    background-position: top;	         /* 背景图片的位置 */\
    padding-right: 0px;	    /*内边距设置*/	    \
    padding-left: 0px;		/*  */	\
    padding-bottom: 10px;	/*  */   \
    padding-top: 10px;		/*  */    \
    background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
    /* 按钮文本设置 */\
    text-align: bottom;			             /* 文本的位置 */\
    font-size: 22px;\
    qproperty-iconSize: 20px 20px;			/* 设置图标大小*/\
color: rgb(125, 255, 250);			                 /* 文本颜色 */\
}"
"QPushButton:pressed{background-color:rgb(99,211,133);}"
"QPushButton:checked{background-color:rgb(66,66,99);}"
    );
//        newbutton->setIconSize(QSize(1,2));

}
btnlist_ui.append(theBtn[0]);
btnlist_ui.append(theBtn[1]);
theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/切换.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/切换2.png); /* 背景图片 */"+"}");
theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

btnlist_ui.at(0)->setGeometry(130,300,120,90 );
btnlist_ui.at(1)->setGeometry(390,300,120,90 );
btnlist_ui.at(0)->setText(tr("修改"));
btnlist_ui.at(1)->setText(tr("返回"));

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}

void Density_set::Recvie_O3qxdd(QString s)
{
    if(Sysset->Sys_Run[1]!=3)
    {
        if(s.toFloat()>=0 && s.toFloat()<=100)
        {
            Sysset->Ozo_Span[O3_densityNum-1]=s.toFloat();
            theLabel[2]->setText(QString::number(s.toFloat()));
        }
        else if(s.toFloat() >100 )
        {
            Sysset->Ozo_Span[O3_densityNum-1]=100;
            theLabel[2]->setText("100");
        }
        else if(s.toFloat() < 0)
        {
            Sysset->Ozo_Span[O3_densityNum-1]=0;
            theLabel[2]->setText("0");
        }
    }
    else if(Sysset->Sys_Run[1]==3)
        {
        float  O3_data = s.toFloat();

            Sysset->Set_Ozo_Con[O3_densityNum-1]=s.toFloat();
            theLabel[2]->setText(QString::number(s.toFloat()));

             if(O3_data >5)
            {
                Sysset->Set_Ozo_Con[O3_densityNum-1]=5;
                theLabel[2]->setText("5");
            }
             if(O3_data <0)
            {
                Sysset->Set_Ozo_Con[O3_densityNum-1]=0;
                theLabel[2]->setText("0");
            }

        }
    Sysctrl->Para_Save  =1;
}

bool Density_set::eventFilter(QObject *watched, QEvent *event)
{
    if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched)
{
    if(event->type() == QEvent::KeyPress)
    {
        QKeyEvent *ev = (QKeyEvent *)event;
        QPushButton *button = (QPushButton *)watched;

       {
           switch (ev->key())
           {
           case OK:
           {
               emit  button-> clicked();
             break;
           }
           case UP: //Up
             {

               focusPreviousChild();
               break;
             }
           case DOWN: //Down
             {
              focusNextChild();
               break;
             }
           case BACK:
           {
               this->parentWidget()->show();
               this->close();
               break;
           }
           case INIT:
           {
               init_Show *show = new init_Show(this);
               show->show();
               this->hide();
               break;
           }
            default:
               break;
           }
       }
    }
    else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
              {
                  QPushButton *focus_button = (QPushButton *)watched;
                          focus_button->setFixedSize(160,120);
//                                  focus_button->setStyleSheet(focusBtn[2]->styleSheet());
//                                  focus_button->setIconSize(QSize(1,2));
              }
    else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
             {
        QPushButton *focus_button = (QPushButton *)watched;
                focus_button->setFixedSize(120,90);
             }
    }
    return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}
